import React, { useState } from 'react';
import { notification } from 'antd';
import useAuth from '../contexts/useAuth';

// 职场沟通助手登录组件
function Login({ onSwitchToRegister }) {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [error, setError] = useState('');
  const [success, setSuccess] = useState('');
  const [loading, setLoading] = useState(false);
  const { login } = useAuth();

  const handleSubmit = async (e) => {
    e.preventDefault();
    setError('');
    setSuccess('');
    setLoading(true);

    try {
      const result = await login(email, password);
      if (!result.success) {
        const errorMessage = result.error || '登录失败，请重试';
        setError(errorMessage);
        notification.error({
          message: '登录失败',
          description: errorMessage,
          placement: 'topRight',
        });
      } else {
        setSuccess('登录成功！正在跳转...');
        console.log('登录成功');
        notification.success({
          message: '登录成功',
          description: '正在跳转至首页...',
          placement: 'topRight',
        });
      }
    } catch (error) {
      const errorMessage = error.message || '发生错误，请重试';
      setError(errorMessage);
      console.error('登录失败:', error);
      notification.error({
        message: '登录失败',
        description: errorMessage,
        placement: 'topRight',
      });
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="auth-container">
      <div className="auth-card">
        <div className="auth-header">
          <h2>欢迎回来</h2>
          <p>登录您的账户以继续使用</p>
        </div>
        
        <form onSubmit={handleSubmit} className="auth-form">
          {error && <div className="auth-message auth-error">{error}</div>}
          {success && <div className="auth-message auth-success">{success}</div>}
          
          <div className="form-group">
            <label htmlFor="email">邮箱</label>
            <input
              type="email"
              id="email"
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              placeholder="请输入邮箱"
              required
              disabled={loading}
            />
          </div>
          
          <div className="form-group">
            <label htmlFor="password">密码</label>
            <input
              type="password"
              id="password"
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              placeholder="请输入密码"
              required
              disabled={loading}
            />
          </div>
          
          <button 
            type="submit" 
            className="auth-button" 
            disabled={loading}
          >
            {loading ? '登录中...' : '登录'}
          </button>
        </form>
        
        <div className="auth-footer">
          <p>
            还没有账户？ 
            <button 
              type="button" 
              onClick={onSwitchToRegister}
              className="auth-switch-button"
            >
              立即注册
            </button>
          </p>
        </div>
      </div>
    </div>
  );
}

export default Login;